/**
 * Created by Administrator on 2017/3/1 0001.
 */
document.addEventListener('Main_LoadComplete',gameStart);

var ratio=window.devicePixelRatio;
var pixiStageW=1136;
var pixiStageH=640;
var renderer=PIXI.autoDetectRenderer(pixiStageW,pixiStageH,{antialias:true,transparent:true,resolution:1});
var stage=new PIXI.Stage(0xFF0000);
var resource;

var div_pixi=document.createElement('div');
document.body.appendChild(div_pixi);
div_pixi.appendChild(renderer.view);

var preload=Preload.create();
preload.init(stage,pixiStageW,pixiStageH);

// var con_whole=new PIXI.Container();
// con_whole.interactive=true;
// stage.addChild(con_whole);
// var pd=ParticleDemo.create();
// stage.addChild(pd.emitterContainer);
pageVer=new PIXI.DisplayObjectContainer();

var sp_verbg =new PIXI.Graphics();
sp_verbg.beginFill(0xffffff,1);
sp_verbg.drawRect(0,0,1136,640);
sp_verbg.endFill();
pageVer.addChild(sp_verbg);

var sp_help =new PIXI.Sprite.fromImage('assets/images/verHelp.png');
sp_help.anchor.set(0.5);
sp_help.x=1136/2;
sp_help.y=640/2-120;
pageVer.addChild(sp_help);

TweenMax.to(sp_help,2,{rotation:180*Math.PI/180,repeatDelay:0.5,repeat:-1,yoyo:true,ease:Back.easeInOut});


var sp_vert0 =new PIXI.Sprite.fromImage('assets/images/ver_t0.png');
sp_vert0.anchor.set(0.5);
sp_vert0.scale.x=sp_vert0.scale.y=0.8
sp_vert0.x=1136/2;
sp_vert0.y=640/2+120;
pageVer.addChild(sp_vert0);

var loadComplete;
var game;


function gameStart(e) {

    console.log('.............')
    loadComplete=true;
    resource=e.detail.resources;
    resizeHandler();
    // var game=Game.create();
    // stage.addChildAt(game.ui,0);
}

resizeHandler();
window.onresize = resizeHandler;
function resizeHandler() {
    renderer.resize(window.innerWidth, window.innerHeight);
    mode='noBorder';
    switch (mode) {
        case 'exactFit':
            stage.scale.x = window.innerWidth / pixiStageW;
            stage.scale.y = window.innerHeight / pixiStageH;
            break;
        case 'noBorder':
            stage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerWidth / pixiStageW) : (window.innerHeight / pixiStageH);
            stage.scale.y = stage.scale.x;
            break;
        case 'noScale':
            stage.scale.x = 1;
            stage.scale.y = 1;
            break;
        case 'showAll':
            stage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerHeight / pixiStageH) : (window.innerWidth / pixiStageW);
            stage.scale.y = stage.scale.x;
            break;
    }
    // alert(window.innerWidth);
    // alert(window.innerHeight)
    stage.x = (window.innerWidth - pixiStageW * stage.scale.x) * .5;
    stage.y = (window.innerHeight - pixiStageH * stage.scale.y) * .5;

    if(window.innerWidth<window.innerHeight)
    {
        if(pageVer&&loadComplete)
        {
            stage.addChild(pageVer);

        }
        if(loadComplete&&!game)
        {
            game=Game.create();
            stage.addChildAt(game.ui,0);
        }

    }
    else
    {
        if(pageVer&&pageVer.parent)
        {
            stage.removeChild(pageVer);

        }
        if(loadComplete&&!game)
        {
            game=Game.create();
            stage.addChildAt(game.ui,0);
        }
    }

};

var globalID;
globalID=requestAnimationFrame(animate);
// cancelAnimationFrame(globalID);
function animate() {
    requestAnimationFrame(animate);
    renderer.render(stage);
}